package com.njitt.bysj.controller;

import com.njitt.bysj.base.BaseController;
import com.njitt.bysj.base.ResultInfo;
import com.njitt.bysj.bean.*;
import com.njitt.bysj.query.*;
import com.njitt.bysj.srevice.PurchaseService;
import com.njitt.bysj.srevice.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/13 15:12
 */
@Controller
@RequestMapping("supplier")
public class SupplierController extends BaseController {
    @Resource
    private SupplierService supplierService;

    @Resource
    private PurchaseService purchaseService;

    @RequestMapping("toSupplierPage")
    public String toProviderPage() {
        return "supplier/supplier";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> list(SupplierQuery supplierQuery) {
        return supplierService.supplierListByQuery(supplierQuery);
    }

    @RequestMapping("toMaterialPage")
    public String toMaterialPage() {
        return "supplier/material";
    }

    @RequestMapping("material_list")
    @ResponseBody
    public Map<String, Object> material_list(MaterialQuery materialQuery) {
        return supplierService.materialListByQuery(materialQuery);
    }

    @RequestMapping("toAddOrUpdateMaterial")
    public String addMaterial() {
        return "supplier/addMaterial";
    }

    @RequestMapping("addMaterial")
    @ResponseBody
    public ResultInfo addMaterial(MaterialBean materialBean) {
        supplierService.addMaterial(materialBean);
        return success("msg");
    }

    @RequestMapping("toAddSupplierPage")
    public String toAddOrUpDateSupplier() {
        return "supplier/addSupplier";
    }

    @RequestMapping("queryMainMaterialInSupplier")
    @ResponseBody
    public List<Map<String, Object>> queryMainMaterialInSupplier(Integer supplierId) {
        return supplierService.queryMainMaterialInSupplier(supplierId);
    }

    @RequestMapping("addSupplier")
    @ResponseBody
    public ResultInfo addSupplier(SupplierBean supplierBean) {
        supplierService.addSupplier(supplierBean);
        return success("success");
    }

    @RequestMapping("toLinkManPage")
    public String toLinkManPage(Integer id, HttpServletRequest request) {
        System.out.println(id);
        request.setAttribute("supplierId", id);
        return "supplier/supplierLink";
    }

    @RequestMapping("linkManBySupplerId")
    @ResponseBody
    public Map<String, Object> linkManBySupplerId(LinkManQuery query) {
        return supplierService.selectLinkManByQuery(query);
    }

    @RequestMapping("toAddOrUpDateLinkMan")
    public String toAddOrUpDateLinkMan(Integer supplierId, Integer id, HttpServletRequest request) {
        request.setAttribute("supplierId", supplierId);
        if (id != null) {
            LinkManBean linkManBean = supplierService.selectLinkManBeanById(id);
            request.setAttribute("linkMan", linkManBean);
        }
        return "supplier/addOrUpdateLinkMan";
    }

    @RequestMapping("addLinkMan")
    @ResponseBody
    public ResultInfo addLinkMan(LinkManBean linkManBean) {
        supplierService.addLinkMan(linkManBean);
        return success("success");
    }

    @RequestMapping("updateLinkMan")
    @ResponseBody
    public ResultInfo updateLinkMan(LinkManBean linkManBean) {
        supplierService.updateLinkMan(linkManBean);
        return success("success");
    }

    @RequestMapping("deleteLinkManByIds")
    @ResponseBody
    public ResultInfo deleteLinkManByIds(Integer[] ids) {
        supplierService.deleteLinkManByIds(ids);
        return success("success");
    }

    @RequestMapping("toUpdateSupplier")
    public String toUpdateSupplier(Integer id, HttpServletRequest request) {
        SupplierBean supplierBean = supplierService.selectSupplierById(id);
        request.setAttribute("supplier", supplierBean);
        return "supplier/updateSupplier";
    }

    @RequestMapping("updateSupplier")
    @ResponseBody
    public ResultInfo updateSupplier(SupplierBean supplierBean) {
        supplierService.updateSupplier(supplierBean);
        return success("success");
    }

    @RequestMapping("toMaterialListPage")
    public String toMaterialPage(Integer id, HttpServletRequest request) {
        SupplierBean supplierBean = supplierService.selectSupplierById(id);
        request.setAttribute("supplier", supplierBean);
        return "supplier/materialList";
    }

    @RequestMapping("materialBySupplier")
    @ResponseBody
    public Map<String, Object> materialBySupplier(MaterialQuery query) {
        return supplierService.materialListBySupplierQuery(query);
    }

    @RequestMapping("toAddMaterBySupplier")
    public String toAddMaterBySupplier(Integer supplierId, HttpServletRequest request) {
        request.setAttribute("supplier", supplierService.selectSupplierById(supplierId));
        return "supplier/addMaterBySupplier";
    }

    @RequestMapping("materialSelected")
    @ResponseBody
    public List<Map<String, Object>> materialSelected() {
        return supplierService.materialSelected();
    }

    @RequestMapping("addMaterialBySupplier")
    @ResponseBody
    public ResultInfo addMaterialBySupplier(SupplierMaterialBean supplierMaterialBean) {
        supplierService.addMaterialBySupplier(supplierMaterialBean);
        return success("success");
    }

    @RequestMapping("toPurchasePage")
    public String toPurchasePage() {
        return "supplier/purchase";
    }

    @RequestMapping("selectParamsPurchaseProject")
    @ResponseBody
    public Map<String, Object> selectParams(PurchaseQuery query) {
        return purchaseService.queryAllPurchaseProject(query);
    }

    @RequestMapping("toPurchaseProject")
    public String toPurchaseProject() {
        /**
         * @description:项目负责
         * @return:  * @param
         * @author:
         * @time: 2022/4/17 21:57
         */
        return "supplier/purchaseProject";
    }

    @RequestMapping("toDetailMaterial")
    public String toDetailMaterial(String projectNo, HttpServletRequest request) {
        ProjectBean projectBean = supplierService.selectProjectByProjectNo(projectNo);
        request.setAttribute("project", projectBean);
        System.out.println(projectBean);
        return "supplier/detailMaterial";
    }

    @RequestMapping("assignPurchaser")
    public String assignPurchaser(Integer projectId, HttpServletRequest request) {
        /**
         * @description: 指派采购负责人
         * @return:  * @param
         * @author:
         * @time: 2022/4/18 10:02
         */
        ProjectPurchaseBean bean = purchaseService.selectByProjectId(projectId);
        request.setAttribute("purchase", bean);
        return "supplier/purchaseAssign";
    }

    @RequestMapping("updateEmpByProjectPurchase")
    @ResponseBody
    public ResultInfo updateEmpByProjectPurchase(ProjectPurchaseBean projectPurchaseBean) {
        purchaseService.updatePurchaseByProjectId(projectPurchaseBean);
        return success("success");
    }

    @RequestMapping("addPurchaseOrder")
    public String addPurchaseOrder(String projectNo, HttpServletRequest request) {
        ProjectBean projectBean = supplierService.selectProjectByProjectNo(projectNo);
        request.setAttribute("project", projectBean);
        return "supplier/addPurchaseOrder";
    }

    @RequestMapping("selectSupplierToSelect")
    @ResponseBody
    public List<Map<String, Object>> selectSupplierToSelect() {
        /**
         * @description: 供应商下拉框
         * @return:  * @param
         * @author:
         * @time: 2022/4/18 16:47
         */
        return supplierService.selectSupplierToSelect();
    }

    @RequestMapping("selectMaterialBySupplerIdToAddPurchaseOrder")
    @ResponseBody
    public List<Map<String, Object>> selectMaterialBySupplerIdToAddPurchaseOrder(Integer supplierId) {
        return supplierService.selectMaterialBySupplerIdToAddPurchaseOrder(supplierId);
    }

    @RequestMapping("selectMaterialNameByMaterialIdToAddPurchaseOrder")
    @ResponseBody
    public String selectMaterialNameByMaterialIdToAddPurchaseOrder(String materialNo) {
        return supplierService.selectMaterialNameByMaterialIdToAddPurchaseOrder(materialNo);
    }

    @RequestMapping("addPurchaseOrderRequest")
    @ResponseBody
    public ResultInfo addPurchaseOrderRequest(PurchaseRequestBean purchaseRequestBean) {
        supplierService.addPurchaseRequestOrder(purchaseRequestBean);
        return success("success");
    }

    @RequestMapping("selectPurchaseRequest")
    @ResponseBody
    public Map<String, Object> selectPurchaseRequest(PurchaseRequestQuery requestQuery) {
        return supplierService.selectPurchaseRequest(requestQuery);
    }

    @RequestMapping("assignPurchaseRequestOrder")
    @ResponseBody
    public ResultInfo assignPurchaseRequestOrder(@RequestBody List<PurchaseRequestBean> requestBeanList) {
        /**
         * @description: 审批采购申请单
         * @return:  * @param requestBeanList
         * @author:
         * @time: 2022/4/19 14:02
         */
        supplierService.assignPurchaseRequestOrder(requestBeanList);
        return success("success");
    }


}
